package core;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.jdom.Element;

public class UserEntry extends Savable{
	private float value = 0;
	private String comment = new String();
	private int categoryId = 0;
	private Date date = new Date();
	
	public UserEntry(Element elt){
		super(elt);	
	}

	public void save(boolean saveXml)
	{
		if (super.xmlNode == null){
			System.out.print("Error: xmlNode == null. Can't save UserEntry!\n");
			return;
		}
		xmlNode.setAttribute("catid", Integer.toString(categoryId));
		xmlNode.setAttribute("value", Float.toString(value));
		xmlNode.setAttribute("comment", comment);
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
		xmlNode.setAttribute("date", format.format(date));
		if (saveXml)
			XmlEngine.get().save();
	}

	public float getValue() {
		return value;
	}

	public void setValue(float value) {
		this.value = value;
	}

	public boolean isCredit() {
		return this.value < 0;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		if (comment == null)
			this.comment = new String();
		else
			this.comment = comment;
	}

	public int getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(int categoryId) {
		if (categoryId < 0)
			this.categoryId = 0;
		else
			this.categoryId = categoryId;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		if (this.date == null)
			this.date = new Date();
		else
			this.date = date;
	}
}
